<?php

namespace IMT\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * IMT\UserBundle\Entity\Participant
 *
 * @ORM\Table(name="contact")
 * @ORM\Entity
 */
class Contact extends Person
{
    /**
     * @ORM\ManyToOne(targetEntity="IMT\UserBundle\Entity\User", inversedBy="profils")
     * @ORM\joinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;
    
    /**
     * @ORM\ManyToMany(targetEntity="IMT\UserBundle\Entity\GroupContact", inversedBy="contacts")
     * @ORM\JoinTable(name="link_contact_groupcontact",
     *      joinColumns={@ORM\JoinColumn(name="groupcontact_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="contact_id", referencedColumnName="id")}
     * )
     */
    private $groups;
    
    /**
     * @ORM\ManyToMany(targetEntity="IMT\AccountBundle\Entity\Account", mappedBy="contacts")
     */
    private $accounts;
    
    /**
     * @ORM\OneToMany(targetEntity="IMT\Plugins\ParticipantsBundle\Entity\ParticipantEvent", mappedBy="participant", cascade={"all"})
     */
    private $events;
    
    /**
     * Constructor
     */
    public function __construct()
    {
    	$this->user = NULL;
    	$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    	$this->accounts = new \Doctrine\Common\Collections\ArrayCollection();
    	$this->events = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    public function getPerson(){
    	return parent::$this;
    }
    
	public function getJsonObject($convert = true){
		$jsonObject = parent::getJsonObject(false);
		
    	return $convert === true ? json_encode($jsonObject) : $jsonObject;
    }
    
    public function setPerson(Person $person){
    	$this->setCivility($person->getCivility());
    	$this->setFirstname($person->getFirstname());
    	$this->setName($person->getName());
    }
    
    /**
     * Add user
     *
     * @param IMT\UserBundle\Entity\User $user
     */
    public function setUser(\IMT\UserBundle\Entity\User $user)
    {
        $this->user = $user;
    }

    /**
     * Get user
     *
     * @return IMT\UserBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Add groups
     *
     * @param IMT\UserBundle\Entity\GroupContact $groupcontact
     */
    public function addGroup(\IMT\UserBundle\Entity\GroupContact $groupcontact)
    {
        $this->groups[] = $groupcontact;
    }

    /**
     * Get groups
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getGroups()
    {
        return $this->groups;
    }

    /**
     * Add accounts
     *
     * @param IMT\AccountBundle\Entity\Account $account
     */
    public function addAccount(\IMT\AccountBundle\Entity\Account $account)
    {
        $this->accounts[] = $account;
    }

    /**
     * Get accounts
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getAccounts()
    {
        return $this->accounts;
    }

    /**
     * Add events
     *
     * @param IMT\Plugins\ParticipantsBundle\Entity\ParticipantEvent $event
     */
    public function addParticipantEvent(\IMT\Plugins\ParticipantsBundle\Entity\ParticipantEvent $event)
    {
        $this->events[] = $event;
    }

    /**
     * Get events
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getEvents()
    {
        return $this->events;
    }
}